//
//  PersonalViewController.m
//  YYZB
//
//  Created by BY_R on 16/3/28.
//
//

#import "PersonalViewController.h"
#import "LogViewController.h"
#import "MineCollectionViewCell.h"
#import "SettingViewController.h"
#import "MessageViewController.h"
#import "UpdateViewController.h"
#import "LoadingView.h"
#import "MyPurseViewController.h"
#import "ServiceSetViewController.h"
#import "RecommendViewController.h"
#import "UseHelpViewController.h"
#import "FeedViewController.h"
#import "AboutUsViewController.h"
#import "NoBundingViewController.h"
#import "CommitInfoViewController.h"

#import "UserInfoController.h"
#import "HelpinfoService.h"
#import "UIImageView+OnlineImage.h"

#import "AgreementViewController.h"
#import "PenaltiesViewController.h"

#import "LawViewController.h"
#import "PolicyViewController.h"
@interface PersonalViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionview;
@property(nonatomic,strong)NSArray * celltableArray;
@property(nonatomic,strong)NSArray * cellimageArray;
@property(nonatomic,strong)NSArray * tableArray;
@property(nonatomic,strong)NSArray * imageArray;
@property(nonatomic,strong)HelpinfoService * helperinfoServer;
@property(nonatomic,strong)NSArray * modelarray;

@property(nonatomic,strong)NSString * Name;
@property(nonatomic,strong)NSString * PhoneNumber;
@property(nonatomic,strong)NSString * EmpiricalValue;
@property(nonatomic,strong)NSString * AvgReviewScore;
@property(nonatomic,strong)NSString * PerDescription;
@property(nonatomic,strong)NSString * WithdrawableMoney;
@property(nonatomic,strong)NSString * UserPhotoPath;
@property(nonatomic,strong)NSString * helperStatus;
@property(nonatomic,strong)NSString * sex;

//fmy添加
@property(nonatomic,strong)NSString * HelperType;  //如果HelperType ==2，就不显示可提现的字样
@property(nonatomic,strong)NSString * currentDay;  //当天是周几
@property(nonatomic,assign)NSInteger  IsAllowSettled;  //当天是周几


@end

@implementation PersonalViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self addBarButton];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake([UIScreen mainScreen].bounds.size.width/3, 90);
    
    self.collectionview.collectionViewLayout = layout;
    
    self.collectionview.dataSource = self;
    self.collectionview.delegate = self;
    self.collectionview.backgroundColor = [UIColor whiteColor];
    
    //注册
    [self.collectionview registerNib:[UINib nibWithNibName:@"MineCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
     self.cellimageArray = @[@"我的钱包",@"服务设置",@"使用帮助",@"建议反馈",@"协议条款",@"奖惩政策",@"推荐有礼"];
    
    self.celltableArray =  @[@"我的钱包",@"服务设置",@"使用帮助",@"建议反馈",@"协议条款",@"奖惩政策",@"推荐有礼"];
    
    
    self.imageArray = @[@"使用帮助",@"建议反馈",@"协议条款",@"奖惩政策",@"推荐有礼",];
    
    
    self.tableArray = @[@"使用帮助",@"建议反馈",@"协议条款",@"奖惩政策",@"推荐有礼",];
    
    [self addPortraitGesture];
    
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appResumed) name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self helpinfoService];
    [self GetCurrentDay];

}

- (void)appResumed {
    [self helpinfoService];
    
}

-(void)helpinfoService{
    self.helperinfoServer = [HelpinfoService new];
    [LoadingView dismiss];
    [LoadingView show];
    [self.helperinfoServer getHelperInfo:[UserInfoController userID] completionHanlder:^(NSArray *items, NSError *error) {

    
        self.Name = [items valueForKey:@"Name"];
        self.sex = [items valueForKey:@"Gender"];
        self.PhoneNumber = [items valueForKey:@"PhoneNumber"];
        self.EmpiricalValue = [items valueForKey:@"EmpiricalValue"];
        self.AvgReviewScore = [items valueForKey:@"AvgReviewScore"];
        self.PerDescription = [items valueForKey:@"PerDescription"];
        self.WithdrawableMoney = [items valueForKey: @"WithdrawableMoney"];
        self.UserPhotoPath = [items valueForKey:@"UserPhotoPath"];
        self.helperStatus = [items valueForKey:@"HelperStatus"];
        
        self.HelperType = [items valueForKey:@"HelperType"];
        self.IsAllowSettled = [[items valueForKey:@"IsAllowSettled"] integerValue];

        [[NSUserDefaults standardUserDefaults]setObject:self.helperStatus forKey:@"status"];
        
        [[NSUserDefaults standardUserDefaults]setObject:[items valueForKey:@"BankAccount"] forKey:@"bankAccount"];
        [[NSUserDefaults standardUserDefaults]setObject:[items valueForKey:@"AccountStatus"] forKey:@"accountStaus"];
        [LoadingView dismiss];
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([self.helperStatus integerValue] == 3
                || [self.helperStatus integerValue] == 0) {
                CommitInfoViewController * vc = [[CommitInfoViewController alloc]init];
                UINavigationController *navConroller = [[UINavigationController alloc] initWithRootViewController:vc];
                [UIApplication sharedApplication].keyWindow.rootViewController = navConroller;
            }
            
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.PortraitImage setOnlineImage:self.UserPhotoPath];
                self.userName.text = self.Name ;
                self.phoneNum.text = [NSString stringWithFormat:@"%@", self.PhoneNumber];
                self.grade.text = [NSString stringWithFormat:@"%@", self.AvgReviewScore];
                self.experience.text = [NSString stringWithFormat:@"%@", self.EmpiricalValue];
                self.personBrief.text = self.PerDescription;
                float money = [self.WithdrawableMoney floatValue];
                self.RMB.text = [NSString stringWithFormat:@"%.2f", money];
                
                self.tixianLabel.text = [self.HelperType integerValue]== 2 ? @"" : @"可提现：";
                self.tixianLabel.hidden = [self.HelperType integerValue]== 2 ? YES : NO;
                self.RMB.hidden = [self.HelperType integerValue] == 2 ? YES : NO ;
                self.yuanLabel.hidden = [self.HelperType integerValue] == 2 ? YES : NO;
            });
//            [self.PortraitImage setOnlineImage:self.UserPhotoPath];
//            self.userName.text = self.Name ;
//            self.phoneNum.text = [NSString stringWithFormat:@"%@", self.PhoneNumber];
//            self.grade.text = [NSString stringWithFormat:@"%@", self.AvgReviewScore];
//            self.experience.text = [NSString stringWithFormat:@"%@", self.EmpiricalValue];
//            self.personBrief.text = self.PerDescription;
//            float money = [self.WithdrawableMoney floatValue];
//            self.RMB.text = [NSString stringWithFormat:@"%.2f", money];
            
            if ([self.sex intValue]== 0) {
                self.gender.image = [UIImage imageNamed:@"男.png"];
            }
            else if([self.sex intValue]== 1){
                self.gender.image = [UIImage imageNamed:@"女.png"];
            }
        });
    }];
    
   
}



//fmy ---- 获取周几 ---
-(void)GetCurrentDay{
    
    self.helperinfoServer = [HelpinfoService new];
    [LoadingView dismiss];
    [LoadingView show];
    [self.helperinfoServer getCurrentDayCompletionHanlder:^(NSString *items, NSError *error) {
        
        [LoadingView dismiss];

        self.currentDay = items;
        NSLog(@"%@",items);
        
    }];
 




}


-(void)addBarButton{
    UIButton * leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)];
    [leftButton setImage:[UIImage imageNamed:@"设置.png"]forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(setting)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIButton*rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,30,30)];
    [rightButton setImage:[UIImage imageNamed:@"消息.png"]forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(messages)forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem= rightItem;
}

-(void)setting
{
    SettingViewController * vc = [[SettingViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)messages{
    MessageViewController * vc = [[MessageViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];

}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==2) {
        return 5;
    }
    else{
        return 7;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MineCollectionViewCell *cell = (MineCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==2) {
        cell.itemImage.image = [UIImage imageNamed:self.imageArray[indexPath.item]];
        cell.itemText.text = self.tableArray[indexPath.item];
    }
    else{
        cell.itemImage.image = [UIImage imageNamed:self.cellimageArray[indexPath.item]];
        cell.itemText.text = self.celltableArray[indexPath.item];
    }
    

    return cell;
}

//self.imageArray = @[@"推荐有礼",@"使用帮助",@"建议反馈",@"关于我们",@"法律协议",@"政策信息"];
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.item == 0) {
        NSString * str = [[NSUserDefaults standardUserDefaults]objectForKey:@"status"];
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==1) {
            if ([str intValue]==0||[str intValue]==1||[str intValue]==3) {
                [LoadingView showToast:@"信息审核中......"];
            }
            else if([str intValue]==2){
                MyPurseViewController * vc = [[MyPurseViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
        else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==2){
            if([str intValue]==2){
                UseHelpViewController * vc = [[UseHelpViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
        
    }
    if (indexPath.item == 1) {
        NSString * str = [[NSUserDefaults standardUserDefaults]objectForKey:@"status"];
        
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==1) {
            if ([str intValue]==0||[str intValue]==1||[str intValue]==3) {
                [LoadingView showToast:@"信息审核中......"];
            }
            else if([str intValue]==2){
                ServiceSetViewController * vc = [[ServiceSetViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
        else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==2){
            if([str intValue]==2){
                FeedViewController * vc = [[FeedViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    }
    if (indexPath.item == 2) {
        NSString * str = [[NSUserDefaults standardUserDefaults]objectForKey:@"status"];
        
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==1) {
            if ([str intValue]==0||[str intValue]==1||[str intValue]==3) {
                [LoadingView showToast:@"信息审核中......"];
            }
            else if([str intValue]==2){
                UseHelpViewController * vc = [[UseHelpViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
        else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==2){
            if([str intValue]==2){
                AgreementViewController * vc = [[AgreementViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
        
    }
    if (indexPath.item == 3) {
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==1) {
            FeedViewController * vc = [[FeedViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==2){
            PenaltiesViewController * vc = [[PenaltiesViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    }
    
    if (indexPath.item == 4) {
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==1) {
            AgreementViewController * vc = [[AgreementViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==2){
            RecommendViewController * vc = [[RecommendViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }

    }
    
    if (indexPath.item == 5) {
        if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==1) {
            PenaltiesViewController * vc = [[PenaltiesViewController alloc]init];
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"type"]intValue]==2){
//            PolicyViewController * vc = [[PolicyViewController alloc]init];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    

    if (indexPath.item == 6) {
        RecommendViewController * vc = [[RecommendViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];

    }


}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:247.0/255 green:247.0/255 blue:247.0/255 alpha:1];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    cell.contentView.backgroundColor = nil;
}

-(void)addPortraitGesture{
    
    self.PortraitImage.userInteractionEnabled = YES;
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goTo)];
    [self.PortraitImage addGestureRecognizer:tap];
    
    UITapGestureRecognizer * RMBtap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goToclick)];
    [self.RMB addGestureRecognizer:RMBtap];
    
}
-(void)goTo{
    UpdateViewController * vc = [[UpdateViewController alloc]init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)goToclick{
    
    NSInteger accountStatus = [[[NSUserDefaults standardUserDefaults]objectForKey:@"accountStaus"]intValue];
    
    if ([_HelperType integerValue] == 1 && accountStatus == 2 && _IsAllowSettled == 1) {
        
        if ([_currentDay isEqualToString:@"星期二"]) {
            
            ServerViewController *cash = ServerViewController.new;
            cash.hidesBottomBarWhenPushed = YES;
            cash.HTMLURL = @"user/cash.html";
            [self.navigationController pushViewController:cash animated:YES];

        }else{
        
            [LoadingView showToast:@"当前日期不可提现"];

        }
        
    }
    
//    if (accountStatus == 2) { //提现页面
//        ServerViewController *cash = ServerViewController.new;
//        cash.hidesBottomBarWhenPushed = YES;
//        cash.HTMLURL = @"user/cash.html";
//        [self.navigationController pushViewController:cash animated:YES];
////        MyPurseViewController * vc = [[MyPurseViewController alloc]init];
////        vc.hidesBottomBarWhenPushed = YES;
////        [self.navigationController pushViewController:vc animated:YES];
//    }
    else if (accountStatus == 1){
        [LoadingView showToast:@"待审核"];
    }
    else if (accountStatus == 0 || accountStatus == 3)
    {
        NoBundingViewController * vc = [[NoBundingViewController alloc]init];
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
